package com.studyyoun.android.nonghe.home.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidlongs.pullrefreshrecyclerylib.common.FullyLinearLayoutManager;
import com.studyyoun.android.androidcommonbaselibrary.fragment.CommonBaseFragment;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.callback.OkhttpCommonCallBack;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.client.OkhttpCommonClient;
import com.studyyoun.android.androidcommonbaselibrary.utils.ToastUtils;
import com.studyyoun.android.nonghe.R;
import com.studyyoun.android.nonghe.home.activity.HomeMsgItemDesActivity;
import com.studyyoun.android.nonghe.home.activity.InformationDetailsActivity;
import com.studyyoun.android.nonghe.home.activity.MoreHelperActivity;
import com.studyyoun.android.nonghe.home.activity.MoreHomeItemActivity;
import com.studyyoun.android.nonghe.home.model.HomeMsgMainModel;
import com.studyyoun.android.nonghe.http.HttpUrlHelper;
import com.studyyoun.android.nonghe.welcom.CommonUserHelper;
import com.studyyoun.android.nonghe.welcom.activity.LoginActivity;
import com.studyyoun.android.nonghe.welcom.model.UserModel;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by androidlongs on 2017/8/27.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

public class HomeFragment extends CommonBaseFragment {


    private RecyclerView mContentRecyclerView;
    private  List<HomeMsgMainModel.DataBean> mDataBeanList=new ArrayList<>();
    private LinearLayout mMoreItemLinearLayout;
    private LinearLayout mSystemLinearLayout;
    private LinearLayout mLoginLinearLayout;
    private LinearLayout mHelperLinearLayout;

    @Override
    protected int getCommonLayoutId() {
        return R.layout.fragment_home_layout;
    }

    @Override
    protected void commonInitView(View mItemView) {

        mContentRecyclerView = mItemView.findViewById(R.id.rv_msg_main_content);

        mMoreItemLinearLayout = mItemView.findViewById(R.id.ll_item_more_click);

        mSystemLinearLayout = mItemView.findViewById(R.id.ll_system_more);

        mLoginLinearLayout = mItemView.findViewById(R.id.ll_login_check);

        mHelperLinearLayout = mItemView.findViewById(R.id.ll_helper_more);
    }

    @Override
    protected void commonFunction() {


    }

    @Override
    protected void commonDelayFunction() {






        mMoreItemLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent lIntent = new Intent(mContext,MoreHomeItemActivity.class);
                lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(lIntent);
            }
        });

        mSystemLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent lIntent = new Intent(mContext,MoreHomeItemActivity.class);
                lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(lIntent);
            }
        });

        mHelperLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent lIntent = new Intent(mContext,MoreHelperActivity.class);
                lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(lIntent);
            }
        });

        mLoginLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel lUserModel = CommonUserHelper.getUserModel();
                if (lUserModel==null){
                    Intent lIntent = new Intent(mContext, LoginActivity.class);
                    lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(lIntent);
                }else {
                    Intent lIntent = new Intent(mContext, InformationDetailsActivity.class);
                    lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(lIntent);
                }
            }
        });

    }


    @Override
    public void onResume() {
        super.onResume();
        String url = HttpUrlHelper.sGetHomeAllClassUrl;
        ConcurrentHashMap<String,String> key = new ConcurrentHashMap<>();
        key.put("pageIndex","1");
        key.put("pageSize","10");

        OkhttpCommonClient.sentPostRequest(url, key, new OkhttpCommonCallBack(HomeMsgMainModel.class) {
            @Override
            public void onSuccess(Object obj) {
                if (obj != null) {
                    HomeMsgMainModel lHomeMsgMainModel = (HomeMsgMainModel) obj;
                    List<HomeMsgMainModel.DataBean> lData = lHomeMsgMainModel.getData();
                    if (lData != null) {
                        mDataBeanList = lData;
                        setMsgDataFunction();
                    }else{
                        ToastUtils.show("暂无最新公告 ",mContext);
                    }
                }else{
                    ToastUtils.show("暂无最新公告 ",mContext);
                }

            }

            @Override
            public void onFaile(Object obj) {
                ToastUtils.show("暂无最新公告 ",mContext);
            }
        });
    }

    private  HomeMsgDataAdapter mHomeMsgDataAdapter;
    private void setMsgDataFunction() {
        if (mHomeMsgDataAdapter==null) {
            mHomeMsgDataAdapter = new HomeMsgDataAdapter(mContext,mDataBeanList);
            mContentRecyclerView.setLayoutManager(new FullyLinearLayoutManager(mContext));
            mContentRecyclerView.setAdapter(mHomeMsgDataAdapter);
        }else {
            mHomeMsgDataAdapter.setListDatas(mDataBeanList);
            mHomeMsgDataAdapter.notifyDataSetChanged();
        }


    }


    public class HomeMsgDataAdapter extends RecyclerView.Adapter<HomeMsgDataHolder> {

        private Context mContext;
        public HomeMsgDataAdapter(Context pContext, List<HomeMsgMainModel.DataBean> pDataBeanList) {
            mDataBeanList = pDataBeanList;
            mContext = pContext;

        }

        @Override
        public HomeMsgDataHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View lView = View.inflate(mContext,R.layout.home_item_layout_1,null);
            return new HomeMsgDataHolder(lView,mContext);
        }

        @Override
        public void onBindViewHolder(HomeMsgDataHolder holder, int position) {

            HomeMsgMainModel.DataBean lDataBean = mDataBeanList.get(position);
            holder.setDatas(lDataBean,position);
        }

        @Override
        public int getItemCount() {
            return mDataBeanList==null ? 0 :mDataBeanList.size();
        }
        private List<HomeMsgMainModel.DataBean> mDataBeanList;

        public void setListDatas(List<HomeMsgMainModel.DataBean> pListDatas) {
            mDataBeanList = pListDatas;
        }
    }


    public class HomeMsgDataHolder extends RecyclerView.ViewHolder{

        private final TextView mItemTextView;

        public HomeMsgDataHolder(View itemView, Context pContext) {
            super(itemView);
            mItemTextView = itemView.findViewById(R.id.tv_item_title);
        }

        public void setDatas(final HomeMsgMainModel.DataBean pDataBean, int pPosition) {

            String lTitle = pDataBean.getTitle();
            if (TextUtils.isEmpty(lTitle)) {
                lTitle="***";
            }
            mItemTextView.setText(lTitle);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent lIntent = new Intent(mContext, HomeMsgItemDesActivity.class);
                    lIntent.putExtra("model",pDataBean);
                    lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(lIntent);
                }
            });
        }
    }
}
